﻿/*
* 班级管理
*/
function SchoolClassIndex() {
    this.urlRoot = ctx + '/SchoolClass/';
    this.tableObj;

}

SchoolClassIndex.prototype.initTable = function () {
    var This = this;

    var h = $(window).height() - 5;
    this.tableObj = $('#listTable').bootstrapTable({
        url: This.urlRoot + 'list',
        height: h,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        toolbar: '#tableTool',
        queryParams: function (params) {
            var pageIndex = params.offset / params.limit + 1

            $('#page').val(pageIndex);
            $('#rows').val(params.limit);
            var searchData = $('#searchForm').serializeJson();
            return searchData;
        },
        columns: [
            {
                field: 'id',//第一列序号
                title: '操作',
                width: '100px',
                align: 'center',
                formatter: function (value, row, index) {
                    var html = '<button class="btn btn-info btn-circle btn-outline" onclick="SchoolClassIndex.dialog(true,' + value + ');"><span class="glyphicon glyphicon-pencil"></span></button>';
                    html = html + '&nbsp;<button class="btn btn-danger btn-circle btn-outline" onclick="SchoolClassIndex.del(' + value + ');"><span class="glyphicon glyphicon-trash"></span></button>';
                    return html;
                }
            },
            {
                field: 'schoolName',
                title: '所属学校',
                align: 'center'
            },
            {
                field: 'entranceYear',
                title: '入学年份',
                align: 'center',
                formatter: function (v, r, i) {
                    if (v == 0) {
                        return null;
                    }
                    return v;
                }
            },
            {
                field: 'className',
                title: '班级名称',
                align: 'center'
            },
            {
                field: 'teacherName',
                title: '班主任',
                align: 'center'
            },
            {
                field: 'classStudentNum',
                title: '人数',
                align: 'center'
            },
            {
                field: 'remark',
                title: '备注',
                align: 'center',
                formatter: function (v, r, i) {
                    if (v != null && v.length > 20) {
                        return '<span title="'+v+'">'+v.substr(0,20)+'...</span>';
                    }
                    return v;
                }
            }, {
                field: 'updateTime',
                title: '最后修改时间',
                align: 'center'
            }, {
                field: 'updateUser',
                title: '最后修改人',
                align: 'center'
            }, {
                field: 'createTime',
                title: '创建时间',
                align: 'center'
            }, {
                field: 'createUser',
                title: '创建人',
                align: 'center'
            }
        ]
    });
}

/*
打开编辑窗口，是否更新，更新加载的数据
*/
SchoolClassIndex.dialog = function (isUpdate, id) {
    var title = '添加班级';
    var url = '../SchoolClass/dialog';
    if (isUpdate) {
        title = "修改班级";
        url = '../SchoolClass/dialog?id=' + id;
    }

    window.parent.layer.open({
        title: title,
        type: 2,
        content: url,
        scroll: true,
        btn: [],
        area: ['800px', '500px'],
        cancel: function () {
            $('#listTable').bootstrapTable('refresh');
        }
    });
}

SchoolClassIndex.prototype.getSelectRow = function () {
    var selectRows = this.tableObj.bootstrapTable('getSelections');
    if (selectRows.length == 0) {
        layer.alert('请选择一行数据', { icon: 6 });
        return null;
    }
    return selectRows[0];
}

SchoolClassIndex.prototype.reloadTable = function () {
    this.tableObj.bootstrapTable('refresh');
}

SchoolClassIndex.prototype.bind = function () {
    var This = this;

    $('#add').click(function () {
        SchoolClassIndex.dialog(false, null);
    });

    $('#search').click(function () {
        This.reloadTable();
    });
    $('#resetForm').click(function () {
        $('#searchForm')[0].reset();
    });
}

SchoolClassIndex.del = function (id) {
    var del = function () {
        var loadTitleIndex = layer.msg('删除中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
        $.post(ctx + '/SchoolClass/del', { id: id }, function (server) {
            layer.close(loadTitleIndex);
            if (server.Statu == 0) {
                $('#listTable').bootstrapTable('refresh');
                layer.msg('删除成功', { icon: 1 });
            } else {
                layer.alert('删除失败', { icon: 5 });
            }
        });
    }

    layer.open({
        content: '确认删除吗？',
        btn: ['删除', '取消'],
        icon: 2,
        yes: function (index, layero) {
            del();
        }
    });
}

$(function () {
    var obj = new SchoolClassIndex();
    obj.bind();
    obj.initTable();
});